package Prova;

import ProvaView.HTMLDAO;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class HTMLGenerator implements HTMLDAO
{
    public boolean imprimirProva(List<QuestaoProva> prova)
    {
        boolean imprime = true;
        try
        {
            FileWriter file = null;
            try
            {
                file = new FileWriter("prova.html");
                file.write("<html>\n<head>\n<h1>Prova</h1>\n</head>\n<body align=left>\n");

                for (int i = 1; i <= prova.size(); i++)
                {
                    for (QuestaoProva p : prova)
                    {
                        if (i == p.getPosicao())
                        {
                            file.write("<strong>Valor da Questao: </strong>" + p.getPontuacao());
                            file.write("<strong><br />Enunciado: </strong>");
                            String[] questaoFormatada = p.questaoFormatada();
                            file.write("<strong><br />\n" + i + ") " + questaoFormatada[0] + "</strong>");

                            if (!(p.getTipoQuestao().equalsIgnoreCase("Discursiva")))
                            {
                                char alternativas = 'a';
                                for (int j = 1; j < questaoFormatada.length; j++)
                                    file.write("<br />\n " + (char) (alternativas + (j-1))+ ") " + questaoFormatada[j]);
                            }

                            file.write("<br />");
                            file.write("<br />");
                        }
                    }
                }

                file.write("\n</body>\n</html>");
                file.close();

                return imprime;
            } 
            finally
            {
                if (file != null)
                    file.close();
                
                return imprime;
            }
        }
        catch (IOException e)
        {
            imprime = false;
            System.err.println("Erro no arquivo");
        }
        return imprime;
    }

    public boolean imprimirGabarito(List<QuestaoProva> resposta)
    {
        boolean imprime = true;

        try
        {
            FileWriter file = null;
            try
            {
                file = new FileWriter("gabarito.html");
                file.write("<html>\n<head>\n<h1>Prova com Gabarito</h1>\n</head>\n<body align=left>\n");
                for (int i = 1; i <= resposta.size(); i++)
                {
                    for (QuestaoProva p : resposta)
                    {
                        if (i == p.getPosicao())
                        {
                            file.write("<strong>Valor da Questao: </strong>" + p.getPontuacao());
                            file.write("<strong><br />Enunciado: </strong>");
                            String[] questaoComGabarito = p.questaoFormatadaComGabarito();
                            file.write("<strong><br />\n" + i + ") " + questaoComGabarito[0] + "</strong>");

                            if(p.getTipoQuestao().equalsIgnoreCase("Discursiva"))
                                file.write("<strong><br />Resposta: </strong>"+questaoComGabarito[1]);
                            
                            else
                            {
                                char alternativas = 'a';
                                int j = 1;
                                for (; j < (questaoComGabarito.length - 1); j++)
                                    file.write("<br />\n " + (char) (alternativas + (j-1))+ ") " + questaoComGabarito[j]);
                                
                                file.write("<strong><br />Resposta: </strong>"+questaoComGabarito[j]);
                            }

                            file.write("<br />");
                            file.write("<br />");
                        }
                    }
                }

                file.write("\n</body>\n</html>");
                file.close();

                return imprime;
            } 
            finally
            {
                if (file != null)
                    file.close();
                
                return imprime;
            }
        }
        catch (IOException e)
        {
            imprime = false;
            System.err.println("Erro no arquivo");
        }

        return imprime;
    }
}
